package com.jiuyuan.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiuyuan.entity.Admin;
import com.jiuyuan.entity.vo.RespBean;
import com.jiuyuan.entity.vo.RespBeanEnum;
import com.jiuyuan.service.IAdminRoleService;
import com.jiuyuan.service.IAdminService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: hrms
 * @description:
 * @author: Feng
 * @create: 2023-02-20 05:56
 **/

@RestController
@RequestMapping("/admin")
public class AdminInfoController {
    @Autowired
    IAdminService adminService;

    @ApiModelProperty("更新信息")
    @PutMapping("/info")
    public RespBean Info(@RequestBody Admin admin, Authentication authentication){
        if (adminService.updateById(admin)){
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin,null,authentication.getAuthorities()));
            return RespBean.success(RespBeanEnum.SUCCESS);
        }
        return RespBean.error(RespBeanEnum.UPDATE_FILED);
    }

    @ApiModelProperty("更新密码")
    @PutMapping("/pass")
    public RespBean pass(@RequestBody Map<String,Object> map){
        //更新时要进行密码加密
        String oldPass = (String) map.get("oldPass");
        String pass = (String) map.get("pass");
        Integer adminId = (Integer) map.get("adminId");
        return adminService.updatePass(adminId,oldPass,pass);

    }
}
